import logging
from typing import Any

from aiogram import types
from aiogram_dialog import DialogManager
from arq import ArqRedis
from fluentogram import TranslatorRunner

from src.presentation.tg_bot.states import ProxyDetailsSG

logger = logging.getLogger(__name__)


async def select_proxy_id(_, __, manager: DialogManager, proxy_id: str):
    manager.dialog_data["proxy_id"] = int(proxy_id)
    await manager.switch_to(ProxyDetailsSG.proxy_details)


async def check_proxy_speed(call: types.CallbackQuery, _, manager: DialogManager):
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    arqredis: ArqRedis = manager.middleware_data.get("arqredis")
    proxy_id: int = manager.dialog_data.get("proxy_id")

    await arqredis.enqueue_job(
        "check_proxy_speed_job",
        locale=locale,
        user_id=call.from_user.id,
        proxy_id=proxy_id,
    )
    logger.info(f"Добавлена задача по проверке скорости прокси №{proxy_id}")

    await call.message.answer(locale.get("in-work-msg"))
    # await call.answer("")
    await manager.done()


async def show_ordered_proxies(_, __, manager: DialogManager):
    order_id: int = manager.dialog_data.get("order_id")

    await manager.start(ProxyDetailsSG.select_proxy, {"order_id": order_id})
